package com.example.yuanboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.yuanboot.common.msg.BaseResultCode;
import com.example.yuanboot.common.msg.ObjectRestResponse;
import com.example.yuanboot.common.msg.ResponseUtils;
import com.example.yuanboot.common.msg.TableResultResponse;
import com.example.yuanboot.model.vo.page.PageInput;
import com.example.yuanboot.model.entity.order.Orders;
import com.example.yuanboot.service.order.OrdersService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 订单
 *
 * @author qingyan 2021-07-20
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {
    private final OrdersService ordersService;

    @GetMapping("/list")
    public TableResultResponse<Orders> list(){
        List<Orders> list = ordersService.ordersList();
        return new TableResultResponse<>(list.size(), list);
    }

    @GetMapping("/page")
    public TableResultResponse<Orders> page(PageInput pageInput) {
        Page<Orders> page = ordersService.page(new Page<>(pageInput.getPage(), pageInput.getSize()));
        return new TableResultResponse<>(page.getTotal(), page.getRecords());
    }

    @DeleteMapping("/{id}/delete")
    public ObjectRestResponse delete(@PathVariable("id") Integer id) {
        return ResponseUtils.r(ordersService.removeById(id), BaseResultCode.SUCCESS);
    }
}
